package tools.clustering.isodata;

import java.io.FileInputStream;
import java.io.ObjectInputStream;

import tools.utils.DoubleMatrix;

public class isodata_2csvDistanceMatrix {
	
	public static void main(String[] args)throws Exception{
		if (args.length==0) {
			System.out.println(printHelp());
			System.exit(616);
		} else {
			if (args[0].equals("toCSV")) {
				if (args.length==2) {
					toCsvDistanceMatrix(args[1]);
				} else {
					System.out.println(printHelp());
					System.exit(616);
				}
			}else if(args[0].equals("another method")){
				
			}else {
				System.out.println(printHelp());
				System.exit(616);
			}
		}
		
		System.exit(0);
	}
	
	public static String printHelp(){
		String help="toCSV - converts a datum-object to csv\n";
		help+="\ttoCSV <infile>\n";
		
		return help;
	}

	private static void toCsvDistanceMatrix(String infile)throws Exception{
		String sep="\t"; 
		ObjectInputStream in=new ObjectInputStream(new FileInputStream(infile));
		isodata_datum datum=(isodata_datum)in.readObject();
		in.close();
		Object[] allIndices=datum.getAllIndices().toArray();
		System.out.print("id_"+allIndices[0]);
		for (int i = 1; i < allIndices.length; i++) {
			System.out.print(sep+"id_"+allIndices[i]);
		}
		System.out.println("");
		for (int i = 0; i < allIndices.length; i++) {
			DoubleMatrix id=(DoubleMatrix)datum.getData((Integer)allIndices[i]);
			System.out.print(transform(id.get(0)));
			for (int j = 1; j < allIndices.length; j++) {
				System.out.print(sep+transform(id.get(j)));
			}
			System.out.println("");
		}
		
//		for (int i = 0; i < allIndices.length; i++) {
//			for (int j = 0; j < allIndices.length; j++) {
//				try{
//				System.out.print(transform(datum.getDistance((Integer)allIndices[i], (Integer)allIndices[j]))+sep);
//				}catch (Exception e) {
//					System.out.println((Integer)allIndices[i]+"\t"+(Integer)allIndices[j]);
//					// TODO: handle exception
//				}
//			}
//			System.out.println("");
//		}
	}
	
	private static double transform(double a){
		return a;
	}
}
